package com.authine.cloudpivot.app.openapi.gateway;

import com.authine.cloudpivot.app.openapi.domain.gateway.AppConfigValueGateway;
import com.authine.cloudpivot.app.openapi.domain.openapi.AppConfigValue;
import com.authine.cloudpivot.app.openapi.domain.openapi.UserInfoValue;
import com.authine.cloudpivot.app.openapi.repository.OpenapiAppConfigDORepository;
import com.authine.cloudpivot.app.openapi.repository.OpenapiAppLoginDORepository;
import com.authine.cloudpivot.app.openapi.repository.databaseobject.OpenapiAppConfigDO;
import com.authine.cloudpivot.app.openapi.repository.databaseobject.OpenapiAppLoginDO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.Objects;

/**
 * @author lifuchun
 */
@Component
public class OpenApiGatewayImpl implements AppConfigValueGateway {

    private final  RpcManager rpcManager;

    private final OpenapiAppConfigDORepository openapiAppConfigDORepository;

    private final OpenapiAppLoginDORepository openapiAppLoginDORepository;

    public OpenApiGatewayImpl(RpcManager rpcManager, OpenapiAppConfigDORepository openapiAppConfigDORepository, OpenapiAppLoginDORepository openapiAppLoginDORepository) {
        this.rpcManager = rpcManager;
        this.openapiAppConfigDORepository = openapiAppConfigDORepository;
        this.openapiAppLoginDORepository = openapiAppLoginDORepository;
    }


    /**
     * 记录通过openAPI登录过的用户
     *
     * @param userId 用户id
     * @param appKey 应用key
     */
    @Override
    public void recordTokenUserInfo(String userId, String appKey) {

        Condition condition = new Condition(OpenapiAppLoginDO.class);

        condition.createCriteria().andEqualTo("appKey",appKey).andEqualTo("userId",userId);

        boolean  notExist = CollectionUtils.isEmpty(openapiAppLoginDORepository.findByCondition(condition));

        if (notExist) {

            OpenapiAppLoginDO param = new OpenapiAppLoginDO();

            param.setAppKey(appKey);
            param.setUserId(userId);
            param.setMemo("openapi登录");

            openapiAppLoginDORepository.save(param);

        }
    }

    @Override
    public AppConfigValue getAppConfigValue(String appKey) {

        OpenapiAppConfigDO openapiAppConfigDO = openapiAppConfigDORepository.findBy("appKey", appKey);


        if (Objects.isNull(openapiAppConfigDO)){
            throw new IllegalArgumentException(String.format("%s对应的应用配置不存在",appKey));
        }

        AppConfigValue appConfigValue = new AppConfigValue();
        BeanUtils.copyProperties(openapiAppConfigDO,appConfigValue);

        return appConfigValue;
    }

    /**
     * 从org服务取得用户信息
     *
     * @param userId 用户id
     * @return 用户信息
     */
    @Override
    public UserInfoValue getUserInfoValue(String userId,String corpId) {
        return rpcManager.getUserInfoValue(userId,corpId);
    }
}
